package com.ms.game.bank.db;

import java.util.ArrayList;

import com.ms.game.bank.message.SBankBookDetailsResponse;
import com.ms.game.bank.message.SSimpleBankLog;
import com.ms.game.common.OSType;
import com.ms.game.db.dto.TransactionsDetailsUtil;
import com.ms.service.error.ErrorCode;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.task.TaskAsyncCommand;

public class LoadBankBookDetailsAsync extends TaskAsyncCommand{

	public int messageType;
	public long startTime;
	public long endTime;
	public GUID playerID;
	public GUID sessionID;
	public GUID accountID;
	public OSType osType;
	private ArrayList<BankLogModel> models;
	private ArrayList<SSimpleBankLog> simples;
	
	@Override
	public void asyncExecute() throws Exception {
		models = BankDAO.getBankBookDetail(playerID.toString(), startTime, endTime);
		if (models != null && models.size() > 0) {
			simples = new ArrayList<SSimpleBankLog>();

			for (BankLogModel bankLogModel : models) {
				SSimpleBankLog log = TransactionsDetailsUtil.BankLogModelToSimple(bankLogModel);

				simples.add(log);

				if (simples.size() >= TransactionsDetailsUtil.TRANSACTION_DETAIL_TO_CLIENT_LIMIT) {
					break;
				}
			}
		}
		
	}

	@Override
	public void execute() throws Exception {
		SBankBookDetailsResponse response = new SBankBookDetailsResponse();
		if(models == null || models.size() <= 0){
			response.setErrorCode(ErrorCode.SUCCESS);
			MessageAction.sendMessage(sessionID, messageType, response);
			return;
		}
		
		response.bankBookDetails = simples;
		//如果全部減掉送出去的 > 0 代表有剩下的資料
		response.hasDetailLeft = models.size() - simples.size() > 0;
		response.setErrorCode(ErrorCode.SUCCESS);
		MessageAction.sendMessage(sessionID, messageType, response);
	}
}
